package converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import persistence.JPAUtil;

import model.ProblemaChamado;


@FacesConverter(forClass=ProblemaChamado.class)
public class ProblemaConverter implements Converter{

	@Override
	public Object getAsObject(FacesContext arg0, UIComponent arg1, String string) {
		
		if(string == null || string.isEmpty()) return null;	
		Long id = Long.valueOf(string);
		ProblemaChamado problema = JPAUtil.getEntityManager().find(ProblemaChamado.class, id);
		return problema;
	}

	@Override
	public String getAsString(FacesContext arg0, UIComponent arg1, Object object) {
		ProblemaChamado problema = (ProblemaChamado) object;	
		if(problema.getId() == null || problema == null) return null;
		return String.valueOf(problema.getId());
	}

}
